<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Chapter extends CI_Controller {
    private $data = array();

    public function  __construct() {
        parent::__construct();
        //Load Helper
        $this->load->helper("url");
        $this->load->library('pagination'); 
        $this->data['base_url'] = base_url();
        $this->data['root_url'] = $this->config->item('root_url');
        $this->config->set_item('compress_output', TRUE);
        
        //Load Libary
        $this->load->library("ci_smarty");
        
        //$this->output->enable_profiler(TRUE);

        //Load Model
        $this->load->Model("Minfo");
        $this->load->Model("Mcategory");
        $this->load->Model("Mbook");
        $this->load->Model("Mchapter");
        $this->load->Model("Mvolume");
        $this->load->Model("MPromotion");
        $this->load->Model("MSeo");

        //Load Data
        $this->data['dataAll'] = $this->Mcategory->getCategoriesByType(WEB_TYPE);
        $this->data['infoAll'] = $this->Minfo->getInfo();
        $this->data['bookBestList'] = $this->Mbook->getBookPopularBySumViewChapter();
        $this->data['promotion1'] = $this->MPromotion->getPromotionByPos(1);
        $this->data['promotion2'] = $this->MPromotion->getPromotionByPos(2);
        $this->data['seoData'] = $this->MSeo->getCommonSEO(); //Luôn có ở tất cả controller
    }

    public function listChapter($cate_uname, $book_uname, $curPage = 1)
    {
        //Load Category
        $category = $this->Mcategory->getCategoryByUname($cate_uname);
        if (count($category) == 0) {
            $this->ci_smarty->view404();
            return false;
        }
        $this->data['category'] = $category;

        //Load Book
        $book = $this->Mbook->getBookbyUname($book_uname);
        if (count($book) == 0) {
            $this->ci_smarty->view404();
            return false;
        }
        $this->data['book'] = $book;
        
        //increaseView
        $this->Mbook->increaseView($book['id']);
        
        $this->data['main_nav_selected'] = $cate_uname; 
        
        $this->data['listChapterFive'] = $this->Mchapter->getChapterByBookIdByFiveChapterLast($book['id']);

        // config page
        $chapterPageConfig['base_url']      = base_url("truyen-$cate_uname/$book_uname/");
        $chapterPageConfig['total_rows']    = $this->Mchapter->countChapterByBookId($book['id']);
        $chapterPageConfig['per_page']      = 50;
        $chapterPageConfig['uri_segment']   = 3;
        $chapterPageConfig['cur_page']      = $curPage;
        $chapterPageConfig['first_url']     = $this->data['base_url'] . 'truyen-' . $category['uname'] . '/' . $book['uname'] .'.html';

        $curOffset = $chapterPageConfig['per_page'] * ($curPage - 1);

        $this->pagination->initialize($chapterPageConfig);

        $this->data['pageList'] = $this->pagination->create_links();

        // seo template
         if($curPage > 1){
            $bookLink = $this->data['base_url'] . 'truyen-' . $category['uname'] . '/' . $book['uname'] .'/trang-'.$curPage.'.html';
        }else{
            $bookLink = $this->data['base_url'] . 'truyen-' . $category['uname'] . '/' . $book['uname'] .'.html';
        }    
        $bookLink = $this->data['base_url'] . 'truyen-' . $category['uname'] . '/' . $book['uname'] .'.html';
        $title = $book['page_title'];
        $description = $book['page_description'];
        $keyword= $book['page_keyword'];
         
        $this->data['seoData']['title'] = $title;
        $this->data['seoData']['description'] = $description;
        $this->data['seoData']['news_keywords'] = $keyword;
        $this->data['seoData']['canonical'] = $bookLink;

        $this->data['seoData']['og:url'] = $bookLink;
        $this->data['seoData']['og:title'] = $title;
        $this->data['seoData']['thumbnailUrl'] = $this->data['base_url'] . 'public/frontend/hinh-anh/lagre/'.$book['image_filename'];
        $this->data['seoData']['description'] = $description;
         
        $this->data['seoTemplate'] = $this->MSeo->getTemplateSEO($this->data['seoData']);
        
        if($book['use_volume'] == 'Y'){
            //Load Chapter List
            $chapterList = $this->Mchapter->getChaptersByBookIdByVolume($book['id'], $chapterPageConfig['per_page'], $curOffset);

            $volumeList = array();
            foreach ($chapterList as $chapter) {
                $volumeList[$chapter['volume_id']]['volume_name'] = $chapter['volume_name'];
                $volumeList[$chapter['volume_id']]['volume_no'] = $chapter['volume_no'];
                $volumeList[$chapter['volume_id']]['chapterList'][] = $chapter;
            }

            $this->data['volumeList'] = $volumeList;
			
            //Assign var and display template
            $this->ci_smarty->view('frontend/chapter-volume-list.tpl',$this->data); 
        }
        else {
            //Load Chapter List
            $chapterList = $this->Mchapter->getChaptersByBookId($book['id'], $chapterPageConfig['per_page'],$curOffset);
            $this->data['chapterList'] = $chapterList;

            //Assign var and display template
            $this->ci_smarty->view('frontend/chapter-list.tpl',$this->data); 
        }
    }

    public function detailChapter($cate_uname, $book_uname, $chapter_no, $chapter_uname)
    {
        //Load Category
        $category = $this->Mcategory->getCategoryByUname($cate_uname);
        if (count($category) == 0) {
            $this->ci_smarty->view404();
            return false;
        }
        $this->data['category'] = $category;

        //Load Book
        $book = $this->Mbook->getBookbyUname($book_uname);
        if (count($book) == 0) {
            $this->ci_smarty->view404();
            return false;
        }
        $this->data['book'] = $book;

        //Load Chapter
        $chapter = $this->Mchapter->getChapterByNoByUname($cate_uname, $book_uname, $chapter_no, $chapter_uname);
        if (count($chapter) == 0) {
            $this->ci_smarty->view404();
            return false;
        }
        if ($chapter['prev_no'] != NULL) {
            $this->data['prev'] = $this->Mchapter->getChapterByBookIdByNo($chapter['chapter_book_id'], $chapter['prev_no']);
        }
        if ($chapter['next_no'] != NULL) {
            $this->data['next'] = $this->Mchapter->getChapterByBookIdByNo($chapter['chapter_book_id'], $chapter['next_no']);
        }
        $chapter['description'] = nl2br ($chapter['chapter_description']);
        $this->data['chapter'] = $chapter;

        $this->data['main_nav_selected'] = $cate_uname;  
        
        //increaseView
        $this->Mchapter->increaseView($chapter['chapter_id']);
        $this->Mbook->increaseView($book['id']);
        
        // seo template    
    	if($chapter['chapter_no'] > 0)
        {
        	$exitsChapterNo = ' - Chương '.$chapter['chapter_no'];
        }else {
        	$exitsChapterNo = ' - ';
        }
        
        $categoryLink = $this->data['base_url'].'truyen-'.$category['uname'].'/'.$book['uname'].'/chuong-'.$chapter['chapter_no'].'-'.$chapter['chapter_uname'].'.html';
        $title = 'Truyện '.$book['name'] .$exitsChapterNo. ' ' . $chapter['chapter_name'];
        $keyword= $chapter['chapter_page_keyword'];
        $description = $chapter['chapter_page_description'];
         
        $this->data['seoData']['title'] = $title;
        $this->data['seoData']['news_keywords'] = $keyword;
        $this->data['seoData']['description'] = $description;
        $this->data['seoData']['canonical'] = $categoryLink;

        $this->data['seoData']['og:url'] = $categoryLink;
        $this->data['seoData']['og:title'] = $title;
        $this->data['seoData']['thumbnailUrl'] = $this->data['base_url'] . 'public/frontend/hinh-anh/lagre/'.$book['image_filename'];
        $this->data['seoData']['description'] = $description;
         
        $this->data['seoTemplate'] = $this->MSeo->getTemplateSEO($this->data['seoData']);

        //Assign var and display template
        $this->ci_smarty->view('frontend/chapter-detail.tpl',$this->data); 
    }
}